const NProgress = require('nprogress')
import 'nprogress/nprogress.css'
const { default: router } = require('./router')
const { default: store } = require('./store')

NProgress.configure({
  showSpinner: false
})

const whiteList = ['/login']

router.beforeEach((to, from, next) => {
  const token = store.getters.token
  if (token) {
    NProgress.start()
    if (to.path === '/login') {
      next('/')
      NProgress.done()
    } else {
      next()
    }
  } else {
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login')
      NProgress.done()
    }
  }
})

router.afterEach((to, from, next) => {
  NProgress.done()
})
